package org.geektimes.projects.user.web.controller;

import org.eclipse.microprofile.config.Config;
import org.geektimes.context.ComponentContext;
import org.geektimes.projects.user.domain.User;
import org.geektimes.projects.user.service.UserService;
import org.geektimes.projects.user.web.RequestConfigHolder;
import org.geektimes.web.mvc.controller.PageController;

import javax.annotation.Resource;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import java.io.IOException;
import java.util.List;

/**
 * 输出 “Hello,World” Controller
 */
@Path("/")
public class IndexController implements PageController {

    @Resource(name = "bean/UserService")
    private UserService userService;


    public IndexController() {
//        userService = (UserService) ComponentContext.getInstance().getComponent("bean/UserService");
    }

    @Override
    @GET
    @Path("")
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Throwable {
        System.out.println("isAsyncSupported = " + request.isAsyncSupported());
        Config config = RequestConfigHolder.getInstance().getConfig();
        String applicationName = config.getValue("application.name", String.class);
        System.out.println(applicationName);
        request.setAttribute("applicationName", applicationName);
        List<User> users = userService.findAll();
        request.setAttribute("users", users);
        System.out.println(users);
        return "index.jsp";
    }
}
